package stdio.costa.moca;

import javax.persistence.EntityManager;

import stdio.costa.moca.database.DataRepositoryProducer;

public class ThreadLocalEntityManager {

	private static final ThreadLocal<EntityManager> entityManagers = new ThreadLocal<EntityManager>(){
		protected EntityManager initialValue() {
	        return new DataRepositoryProducer().produceEntityManager();
	    }
	};

	public static void setEntityManager(EntityManager user) {
		entityManagers.set(user);
	}

	public static EntityManager getEntityManager() {
		return entityManagers.get();
	}

	public static void clear() {
		entityManagers.get().close();
		entityManagers.remove();
	}
}
